﻿using System;
using System.Collections.Generic;
using System.Text;
using POSPlus.EZBarTender.DatabaseOperations;
using System.Data.SqlClient;

namespace POSPlus.DatabaseOperations
{
	public class ProductBatchesHeader : DatabaseObject
	{
		public String BatchNo { get; set; }
		public String Description { get; set; }
		public DateTime EndDate { get; set; }

		public ProductBatchesHeader(String connectionString)
		{
			ConnectionString = connectionString;
			PopulateDefault();
		}

		public ProductBatchesHeader(String connectionString, String batchNo)
		{
			ConnectionString = connectionString;
			using (SqlConnection connection = new SqlConnection(ConnectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("SELECT BatchNo, Description, EndDate " +
					"FROM ProductBatchesHeader WHERE BatchNo = @BatchNo", connection))
				{
					command.Parameters.AddWithValue("@BatchNo", BatchNo);
					using (SqlDataReader reader = command.ExecuteReader())
					{
						if (reader.Read())
						{
							BatchNo = FromDB<String>(reader["BatchNo"]);
							Description = FromDB<String>(reader["Description"]);
							EndDate = FromDB<DateTime>(reader["EndDate"]);
						}
						else
						{
							PopulateDefault();
						}
						reader.Close();
					}
				}
				connection.Close();
			}
		}

		protected override void PopulateDefault()
		{
			BatchNo = "";
			Description = "";
			EndDate = new DateTime(1900, 1, 1);
		}

		public override Boolean Create()
		{
			throw new NotImplementedException();
		}

		public override Boolean Update()
		{
			throw new NotImplementedException();
		}

		public override Boolean Delete()
		{
			throw new NotImplementedException();
		}

		public static IEnumerable<ProductBatchesHeader> GetAllByBatchNo(String connectionString, string batchNo)
		{
			List<ProductBatchesHeader> result = new List<ProductBatchesHeader>();
			using (SqlConnection connection = new SqlConnection(connectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("SELECT BatchNo,Description,EndDate " +
					"FROM ProductBatchesHeader WHERE BatchNo = @BatchNo", connection))
				{
					command.Parameters.AddWithValue("@BatchNo", batchNo);
					using (SqlDataReader reader = command.ExecuteReader())
					{
						while (reader.Read())
						{
							ProductBatchesHeader header = new ProductBatchesHeader(connectionString);
							header.BatchNo = FromDB<String>(reader["BatchNo"]);
							header.Description = FromDB<String>(reader["Description"]);
							header.EndDate = FromDB<DateTime>(reader["EndDate"]);
							result.Add(header);
						}
						reader.Close();
					}
				}
				connection.Close();
			}
			return result;
		}
	}
}
